home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Objects / intobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  18.4 KB  |  937 lines

  1. /* Integer object implementation */
  2.  
  3. #include "Python.h"
  4. #include <ctype.h>
  5. #include "protos/intobject.h"
  6.  
  7. #ifdef HAVE_LIMITS_H
  8. #include <limits.h>
  9. #endif
  10.  
  11. #ifndef LONG_MAX
  12. #define LONG_MAX 0X7FFFFFFFL
  13. #endif
  14.  
  15. #ifndef LONG_MIN
  16. #define LONG_MIN (-LONG_MAX-1)
  17. #endif
  18.  
  19. #ifndef CHAR_BIT
  20. #define CHAR_BIT 8
  21. #endif
  22.  
  23. #ifndef LONG_BIT
  24. #define LONG_BIT (CHAR_BIT * sizeof(long))
  25. #endif
  26.  
  27. long
  28. PyInt_GetMax()
  29. {
  30.     return LONG_MAX;    /* To initialize sys.maxint */
  31. }
  32.  
  33. /* Standard Booleans */
  34.  
  35. PyIntObject _Py_ZeroStruct = {
  36.     PyObject_HEAD_INIT(&PyInt_Type)
  37.     0
  38. };
  39.  
  40. PyIntObject _Py_TrueStruct = {
  41.     PyObject_HEAD_INIT(&PyInt_Type)
  42.     1
  43. };
  44.  
  45. static PyObject *
  46. err_ovf(msg)
  47.     char *msg;
  48. {
  49.     PyErr_SetString(PyExc_OverflowError, msg);
  50.     return NULL;
  51. }
  52.  
  53. /* Integers are quite normal objects, to make object handling uniform.
  54.    (Using odd pointers to represent integers would save much space
  55.    but require extra checks for this special case throughout the code.)
  56.    Since, a typical Python program spends much of its time allocating
  57.    and deallocating integers, these operations should be very fast.
  58.    Therefore we use a dedicated allocation scheme with a much lower
  59.    overhead (in space and time) than straight malloc(): a simple
  60.    dedicated free list, filled when necessary with memory from malloc().
  61. */
  62.  
  63. #define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  64. #define BHEAD_SIZE    8    /* Enough for a 64-bit pointer */
  65. #define N_INTOBJECTS    ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
  66.  
  67. struct _intblock {
  68.     struct _intblock *next;
  69.     PyIntObject objects[N_INTOBJECTS];
  70. };
  71.  
  72. typedef struct _intblock PyIntBlock;
  73.  
  74. static PyIntBlock *block_list = NULL;
  75. static PyIntObject *free_list = NULL;
  76.  
  77. static PyIntObject *
  78. fill_free_list()
  79. {
  80.     PyIntObject *p, *q;
  81.     /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
  82.     p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
  83.     if (p == NULL)
  84.         return (PyIntObject *) PyErr_NoMemory();
  85.     ((PyIntBlock *)p)->next = block_list;
  86.     block_list = (PyIntBlock *)p;
  87.     p = &((PyIntBlock *)p)->objects[0];
  88.     q = p + N_INTOBJECTS;
  89.     while (--q > p)
  90.         q->ob_type = (struct _typeobject *)(q-1);
  91.     q->ob_type = NULL;
  92.     return p + N_INTOBJECTS - 1;
  93. }
  94.  
  95. #ifndef NSMALLPOSINTS
  96. #define NSMALLPOSINTS        100
  97. #endif
  98. #ifndef NSMALLNEGINTS
  99. #define NSMALLNEGINTS        1
  100. #endif
  101. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  102. /* References to small integers are saved in this array so that they
  103.    can be shared.
  104.    The integers that are saved are those in the range
  105.    -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
  106. */
  107. static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
  108. #endif
  109. #ifdef COUNT_ALLOCS
  110. int quick_int_allocs, quick_neg_int_allocs;
  111. #endif
  112.  
  113. PyObject *
  114. PyInt_FromLong(ival)
  115.     long ival;
  116. {
  117.     register PyIntObject *v;
  118. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  119.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
  120.         (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
  121.         Py_INCREF(v);
  122. #ifdef COUNT_ALLOCS
  123.         if (ival >= 0)
  124.             quick_int_allocs++;
  125.         else
  126.             quick_neg_int_allocs++;
  127. #endif
  128.         return (PyObject *) v;
  129.     }
  130. #endif
  131.     if (free_list == NULL) {
  132.         if ((free_list = fill_free_list()) == NULL)
  133.             return NULL;
  134.     }
  135.     /* PyObject_New is inlined */
  136.     v = free_list;
  137.     free_list = (PyIntObject *)v->ob_type;
  138.     PyObject_INIT(v, &PyInt_Type);
  139.     v->ob_ival = ival;
  140. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  141.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
  142.         /* save this one for a following allocation */
  143.         Py_INCREF(v);
  144.         small_ints[ival + NSMALLNEGINTS] = v;
  145.     }
  146. #endif
  147.     return (PyObject *) v;
  148. }
  149.  
  150. static void
  151. int_dealloc(v)
  152.     PyIntObject *v;
  153. {
  154.     v->ob_type = (struct _typeobject *)free_list;
  155.     free_list = v;
  156. }
  157.  
  158. long
  159. PyInt_AsLong(op)
  160.     register PyObject *op;
  161. {
  162.     PyNumberMethods *nb;
  163.     PyIntObject *io;
  164.     long val;
  165.     
  166.     if (op && PyInt_Check(op))
  167.         return PyInt_AS_LONG((PyIntObject*) op);
  168.     
  169.     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
  170.         nb->nb_int == NULL) {
  171.         PyErr_SetString(PyExc_TypeError, "an integer is required");
  172.         return -1;
  173.     }
  174.     
  175.     io = (PyIntObject*) (*nb->nb_int) (op);
  176.     if (io == NULL)
  177.         return -1;
  178.     if (!PyInt_Check(io)) {
  179.         PyErr_SetString(PyExc_TypeError,
  180.                 "nb_int should return int object");
  181.         return -1;
  182.     }
  183.     
  184.     val = PyInt_AS_LONG(io);
  185.     Py_DECREF(io);
  186.     
  187.     return val;
  188. }
  189.  
  190. PyObject *
  191. PyInt_FromString(s, pend, base)
  192.     char *s;
  193.     char **pend;
  194.     int base;
  195. {
  196.     char *end;
  197.     long x;
  198.     char buffer[256]; /* For errors */
  199.  
  200.     if ((base != 0 && base < 2) || base > 36) {
  201.         PyErr_SetString(PyExc_ValueError, "invalid base for int()");
  202.         return NULL;
  203.     }
  204.  
  205.     while (*s && isspace(Py_CHARMASK(*s)))
  206.         s++;
  207.     errno = 0;
  208.     if (base == 0 && s[0] == '0')
  209.         x = (long) PyOS_strtoul(s, &end, base);
  210.     else
  211.         x = PyOS_strtol(s, &end, base);
  212.     if (end == s || !isalnum(end[-1]))
  213.         goto bad;
  214.     while (*end && isspace(Py_CHARMASK(*end)))
  215.         end++;
  216.     if (*end != '\0') {
  217.   bad:
  218.         sprintf(buffer, "invalid literal for int(): %.200s", s);
  219.         PyErr_SetString(PyExc_ValueError, buffer);
  220.         return NULL;
  221.     }
  222.     else if (errno != 0) {
  223.         sprintf(buffer, "int() literal too large: %.200s", s);
  224.         PyErr_SetString(PyExc_ValueError, buffer);
  225.         return NULL;
  226.     }
  227.     if (pend)
  228.         *pend = end;
  229.     return PyInt_FromLong(x);
  230. }
  231.  
  232. PyObject *
  233. PyInt_FromUnicode(s, length, base)
  234.     Py_UNICODE *s;
  235.     int length;
  236.     int base;
  237. {
  238.     char buffer[256];
  239.     
  240.     if (length >= sizeof(buffer)) {
  241.         PyErr_SetString(PyExc_ValueError,
  242.                 "int() literal too large to convert");
  243.         return NULL;
  244.     }
  245.     if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
  246.         return NULL;
  247.     return PyInt_FromString(buffer, NULL, base);
  248. }
  249.  
  250. /* Methods */
  251.  
  252. /* ARGSUSED */
  253. static int
  254. int_print(v, fp, flags)
  255.     PyIntObject *v;
  256.     FILE *fp;
  257.     int flags; /* Not used but required by interface */
  258. {
  259.     fprintf(fp, "%ld", v->ob_ival);
  260.     return 0;
  261. }
  262.  
  263. static PyObject *
  264. int_repr(v)
  265.     PyIntObject *v;
  266. {
  267.     char buf[20];
  268.     sprintf(buf, "%ld", v->ob_ival);
  269.     return PyString_FromString(buf);
  270. }
  271.  
  272. static int
  273. int_compare(v, w)
  274.     PyIntObject *v, *w;
  275. {
  276.     register long i = v->ob_ival;
  277.     register long j = w->ob_ival;
  278.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  279. }
  280.  
  281. static long
  282. int_hash(v)
  283.     PyIntObject *v;
  284. {
  285.     /* XXX If this is changed, you also need to change the way
  286.        Python's long, float and complex types are hashed. */
  287.     long x = v -> ob_ival;
  288.     if (x == -1)
  289.         x = -2;
  290.     return x;
  291. }
  292.  
  293. static PyObject *
  294. int_add(v, w)
  295.     PyIntObject *v;
  296.     PyIntObject *w;
  297. {
  298.     register long a, b, x;
  299.     a = v->ob_ival;
  300.     b = w->ob_ival;
  301.     x = a + b;
  302.     if ((x^a) < 0 && (x^b) < 0)
  303.         return err_ovf("integer addition");
  304.     return PyInt_FromLong(x);
  305. }
  306.  
  307. static PyObject *
  308. int_sub(v, w)
  309.     PyIntObject *v;
  310.     PyIntObject *w;
  311. {
  312.     register long a, b, x;
  313.     a = v->ob_ival;
  314.     b = w->ob_ival;
  315.     x = a - b;
  316.     if ((x^a) < 0 && (x^~b) < 0)
  317.         return err_ovf("integer subtraction");
  318.     return PyInt_FromLong(x);
  319. }
  320.  
  321. /*
  322. Integer overflow checking used to be done using a double, but on 64
  323. bit machines (where both long and double are 64 bit) this fails
  324. because the double doesn't have enouvg precision.  John Tromp suggests
  325. the following algorithm:
  326.  
  327. Suppose again we normalize a and b to be nonnegative.
  328. Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
  329. Now we test ah and bh against zero and get essentially 3 possible outcomes.
  330.  
  331. 1) both ah and bh > 0 : then report overflow
  332.  
  333. 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
  334.                         negative
  335.  
  336. 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if it's >= 2^31
  337.                         compute al*bl and report overflow if it's negative
  338.                         add (ah*bl)<<32 to al*bl and report overflow if
  339.                         it's negative
  340.  
  341. In case of no overflow the result is then negated if necessary.
  342.  
  343. The majority of cases will be 2), in which case this method is the same as
  344. what I suggested before. If multiplication is expensive enough, then the
  345. other method is faster on case 3), but also more work to program, so I
  346. guess the above is the preferred solution.
  347.  
  348. */
  349.  
  350. static PyObject *
  351. int_mul(v, w)
  352.     PyIntObject *v;
  353.     PyIntObject *w;
  354. {
  355.     long a, b, ah, bh, x, y;
  356.     int s = 1;
  357.  
  358.     a = v->ob_ival;
  359.     b = w->ob_ival;
  360.     ah = a >> (LONG_BIT/2);
  361.     bh = b >> (LONG_BIT/2);
  362.  
  363.     /* Quick test for common case: two small positive ints */
  364.  
  365.     if (ah == 0 && bh == 0) {
  366.         x = a*b;
  367.         if (x < 0)
  368.             goto bad;
  369.         return PyInt_FromLong(x);
  370.     }
  371.  
  372.     /* Arrange that a >= b >= 0 */
  373.  
  374.     if (a < 0) {
  375.         a = -a;
  376.         if (a < 0) {
  377.             /* Largest negative */
  378.             if (b == 0 || b == 1) {
  379.                 x = a*b;
  380.                 goto ok;
  381.             }
  382.             else
  383.                 goto bad;
  384.         }
  385.         s = -s;
  386.         ah = a >> (LONG_BIT/2);
  387.     }
  388.     if (b < 0) {
  389.         b = -b;
  390.         if (b < 0) {
  391.             /* Largest negative */
  392.             if (a == 0 || (a == 1 && s == 1)) {
  393.                 x = a*b;
  394.                 goto ok;
  395.             }
  396.             else
  397.                 goto bad;
  398.         }
  399.         s = -s;
  400.         bh = b >> (LONG_BIT/2);
  401.     }
  402.  
  403.     /* 1) both ah and bh > 0 : then report overflow */
  404.  
  405.     if (ah != 0 && bh != 0)
  406.         goto bad;
  407.  
  408.     /* 2) both ah and bh = 0 : then compute a*b and report
  409.                    overflow if it comes out negative */
  410.  
  411.     if (ah == 0 && bh == 0) {
  412.         x = a*b;
  413.         if (x < 0)
  414.             goto bad;
  415.         return PyInt_FromLong(x*s);
  416.     }
  417.  
  418.     if (a < b) {
  419.         /* Swap */
  420.         x = a;
  421.         a = b;
  422.         b = x;
  423.         ah = bh;
  424.         /* bh not used beyond this point */
  425.     }
  426.  
  427.     /* 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if
  428.                    it's >= 2^31
  429.                         compute al*bl and report overflow if it's negative
  430.                         add (ah*bl)<<32 to al*bl and report overflow if
  431.                         it's negative
  432.             (NB b == bl in this case, and we make a = al) */
  433.  
  434.     y = ah*b;
  435.     if (y >= (1L << (LONG_BIT/2 - 1)))
  436.         goto bad;
  437.     a &= (1L << (LONG_BIT/2)) - 1;
  438.     x = a*b;
  439.     if (x < 0)
  440.         goto bad;
  441.     x += y << (LONG_BIT/2);
  442.     if (x < 0)
  443.         goto bad;
  444.  ok:
  445.     return PyInt_FromLong(x * s);
  446.  
  447.  bad:
  448.     return err_ovf("integer multiplication");
  449. }
  450.  
  451. static int
  452. i_divmod(x, y, p_xdivy, p_xmody)
  453.     register PyIntObject *x, *y;
  454.     long *p_xdivy, *p_xmody;
  455. {
  456.     long xi = x->ob_ival;
  457.     long yi = y->ob_ival;
  458.     long xdivy, xmody;
  459.     
  460.     if (yi == 0) {
  461.         PyErr_SetString(PyExc_ZeroDivisionError,
  462.                 "integer division or modulo");
  463.         return -1;
  464.     }
  465.     if (yi < 0) {
  466.         if (xi < 0) {
  467.             if (yi == -1 && -xi < 0) {
  468.                 /* most negative / -1 */
  469.                 err_ovf("integer division");
  470.                 return -1;
  471.             }
  472.             xdivy = -xi / -yi;
  473.         }
  474.         else
  475.             xdivy = - (xi / -yi);
  476.     }
  477.     else {
  478.         if (xi < 0)
  479.             xdivy = - (-xi / yi);
  480.         else
  481.             xdivy = xi / yi;
  482.     }
  483.     xmody = xi - xdivy*yi;
  484.     if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
  485.         xmody += yi;
  486.         xdivy -= 1;
  487.     }
  488.     *p_xdivy = xdivy;
  489.     *p_xmody = xmody;
  490.     return 0;
  491. }
  492.  
  493. static PyObject *
  494. int_div(x, y)
  495.     PyIntObject *x;
  496.     PyIntObject *y;
  497. {
  498.     long d, m;
  499.     if (i_divmod(x, y, &d, &m) < 0)
  500.         return NULL;
  501.     return PyInt_FromLong(d);
  502. }
  503.  
  504. static PyObject *
  505. int_mod(x, y)
  506.     PyIntObject *x;
  507.     PyIntObject *y;
  508. {
  509.     long d, m;
  510.     if (i_divmod(x, y, &d, &m) < 0)
  511.         return NULL;
  512.     return PyInt_FromLong(m);
  513. }
  514.  
  515. static PyObject *
  516. int_divmod(x, y)
  517.     PyIntObject *x;
  518.     PyIntObject *y;
  519. {
  520.     long d, m;
  521.     if (i_divmod(x, y, &d, &m) < 0)
  522.         return NULL;
  523.     return Py_BuildValue("(ll)", d, m);
  524. }
  525.  
  526. static PyObject *
  527. int_pow(v, w, z)
  528.     PyIntObject *v;
  529.     PyIntObject *w;
  530.     PyIntObject *z;
  531. {
  532. #if 1
  533.     register long iv, iw, iz=0, ix, temp, prev;
  534.     iv = v->ob_ival;
  535.     iw = w->ob_ival;
  536.     if (iw < 0) {
  537.         PyErr_SetString(PyExc_ValueError,
  538.                 "integer to the negative power");
  539.         return NULL;
  540.     }
  541.      if ((PyObject *)z != Py_None) {
  542.         iz = z->ob_ival;
  543.         if (iz == 0) {
  544.             PyErr_SetString(PyExc_ValueError,
  545.                     "pow(x, y, z) with z==0");
  546.             return NULL;
  547.         }
  548.     }
  549.     /*
  550.      * XXX: The original exponentiation code stopped looping
  551.      * when temp hit zero; this code will continue onwards
  552.      * unnecessarily, but at least it won't cause any errors.
  553.      * Hopefully the speed improvement from the fast exponentiation
  554.      * will compensate for the slight inefficiency.
  555.      * XXX: Better handling of overflows is desperately needed.
  556.      */
  557.      temp = iv;
  558.     ix = 1;
  559.     while (iw > 0) {
  560.          prev = ix;    /* Save value for overflow check */
  561.          if (iw & 1) {    
  562.              ix = ix*temp;
  563.             if (temp == 0)
  564.                 break; /* Avoid ix / 0 */
  565.             if (ix / temp != prev)
  566.                 return err_ovf("integer exponentiation");
  567.         }
  568.          iw >>= 1;    /* Shift exponent down by 1 bit */
  569.             if (iw==0) break;
  570.          prev = temp;
  571.          temp *= temp;    /* Square the value of temp */
  572.          if (prev!=0 && temp/prev!=prev)
  573.             return err_ovf("integer exponentiation");
  574.          if (iz) {
  575.             /* If we did a multiplication, perform a modulo */
  576.              ix = ix % iz;
  577.              temp = temp % iz;
  578.         }
  579.     }
  580.     if (iz) {
  581.          PyObject *t1, *t2;
  582.          long int div, mod;
  583.          t1=PyInt_FromLong(ix); 
  584.         t2=PyInt_FromLong(iz);
  585.          if (t1==NULL || t2==NULL ||
  586.              i_divmod((PyIntObject *)t1,
  587.                  (PyIntObject *)t2, &div, &mod)<0)
  588.         {
  589.              Py_XDECREF(t1);
  590.              Py_XDECREF(t2);
  591.             return(NULL);
  592.         }
  593.         Py_DECREF(t1);
  594.         Py_DECREF(t2);
  595.          ix=mod;
  596.     }
  597.     return PyInt_FromLong(ix);
  598. #else
  599.     register long iv, iw, ix;
  600.     iv = v->ob_ival;
  601.     iw = w->ob_ival;
  602.     if (iw < 0) {
  603.         PyErr_SetString(PyExc_ValueError,
  604.                 "integer to the negative power");
  605.         return NULL;
  606.     }
  607.     if ((PyObject *)z != Py_None) {
  608.         PyErr_SetString(PyExc_TypeError,
  609.                 "pow(int, int, int) not yet supported");
  610.         return NULL;
  611.     }
  612.     ix = 1;
  613.     while (--iw >= 0) {
  614.         long prev = ix;
  615.         ix = ix * iv;
  616.         if (iv == 0)
  617.             break; /* 0 to some power -- avoid ix / 0 */
  618.         if (ix / iv != prev)
  619.             return err_ovf("integer exponentiation");
  620.     }
  621.     return PyInt_FromLong(ix);
  622. #endif
  623. }                
  624.  
  625. static PyObject *
  626. int_neg(v)
  627.     PyIntObject *v;
  628. {
  629.     register long a, x;
  630.     a = v->ob_ival;
  631.     x = -a;
  632.     if (a < 0 && x < 0)
  633.         return err_ovf("integer negation");
  634.     return PyInt_FromLong(x);
  635. }
  636.  
  637. static PyObject *
  638. int_pos(v)
  639.     PyIntObject *v;
  640. {
  641.     Py_INCREF(v);
  642.     return (PyObject *)v;
  643. }
  644.  
  645. static PyObject *
  646. int_abs(v)
  647.     PyIntObject *v;
  648. {
  649.     if (v->ob_ival >= 0)
  650.         return int_pos(v);
  651.     else
  652.         return int_neg(v);
  653. }
  654.  
  655. static int
  656. int_nonzero(v)
  657.     PyIntObject *v;
  658. {
  659.     return v->ob_ival != 0;
  660. }
  661.  
  662. static PyObject *
  663. int_invert(v)
  664.     PyIntObject *v;
  665. {
  666.     return PyInt_FromLong(~v->ob_ival);
  667. }
  668.  
  669. static PyObject *
  670. int_lshift(v, w)
  671.     PyIntObject *v;
  672.     PyIntObject *w;
  673. {
  674.     register long a, b;
  675.     a = v->ob_ival;
  676.     b = w->ob_ival;
  677.     if (b < 0) {
  678.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  679.         return NULL;
  680.     }
  681.     if (a == 0 || b == 0) {
  682.         Py_INCREF(v);
  683.         return (PyObject *) v;
  684.     }
  685.     if (b >= LONG_BIT) {
  686.         return PyInt_FromLong(0L);
  687.     }
  688.     a = (unsigned long)a << b;
  689.     return PyInt_FromLong(a);
  690. }
  691.  
  692. static PyObject *
  693. int_rshift(v, w)
  694.     PyIntObject *v;
  695.     PyIntObject *w;
  696. {
  697.     register long a, b;
  698.     a = v->ob_ival;
  699.     b = w->ob_ival;
  700.     if (b < 0) {
  701.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  702.         return NULL;
  703.     }
  704.     if (a == 0 || b == 0) {
  705.         Py_INCREF(v);
  706.         return (PyObject *) v;
  707.     }
  708.     if (b >= LONG_BIT) {
  709.         if (a < 0)
  710.             a = -1;
  711.         else
  712.             a = 0;
  713.     }
  714.     else {
  715.         if (a < 0)
  716.             a = ~( ~(unsigned long)a >> b );
  717.         else
  718.             a = (unsigned long)a >> b;
  719.     }
  720.     return PyInt_FromLong(a);
  721. }
  722.  
  723. static PyObject *
  724. int_and(v, w)
  725.     PyIntObject *v;
  726.     PyIntObject *w;
  727. {
  728.     register long a, b;
  729.     a = v->ob_ival;
  730.     b = w->ob_ival;
  731.     return PyInt_FromLong(a & b);
  732. }
  733.  
  734. static PyObject *
  735. int_xor(v, w)
  736.     PyIntObject *v;
  737.     PyIntObject *w;
  738. {
  739.     register long a, b;
  740.     a = v->ob_ival;
  741.     b = w->ob_ival;
  742.     return PyInt_FromLong(a ^ b);
  743. }
  744.  
  745. static PyObject *
  746. int_or(v, w)
  747.     PyIntObject *v;
  748.     PyIntObject *w;
  749. {
  750.     register long a, b;
  751.     a = v->ob_ival;
  752.     b = w->ob_ival;
  753.     return PyInt_FromLong(a | b);
  754. }
  755.  
  756. static PyObject *
  757. int_int(v)
  758.     PyIntObject *v;
  759. {
  760.     Py_INCREF(v);
  761.     return (PyObject *)v;
  762. }
  763.  
  764. static PyObject *
  765. int_long(v)
  766.     PyIntObject *v;
  767. {
  768.     return PyLong_FromLong((v -> ob_ival));
  769. }
  770.  
  771. static PyObject *
  772. int_float(v)
  773.     PyIntObject *v;
  774. {
  775.     return PyFloat_FromDouble((double)(v -> ob_ival));
  776. }
  777.  
  778. static PyObject *
  779. int_oct(v)
  780.     PyIntObject *v;
  781. {
  782.     char buf[100];
  783.     long x = v -> ob_ival;
  784.     if (x == 0)
  785.         strcpy(buf, "0");
  786.     else
  787.         sprintf(buf, "0%lo", x);
  788.     return PyString_FromString(buf);
  789. }
  790.  
  791. static PyObject *
  792. int_hex(v)
  793.     PyIntObject *v;
  794. {
  795.     char buf[100];
  796.     long x = v -> ob_ival;
  797.     sprintf(buf, "0x%lx", x);
  798.     return PyString_FromString(buf);
  799. }
  800.  
  801. static PyNumberMethods int_as_number = {
  802.     (binaryfunc)int_add, /*nb_add*/
  803.     (binaryfunc)int_sub, /*nb_subtract*/
  804.     (binaryfunc)int_mul, /*nb_multiply*/
  805.     (binaryfunc)int_div, /*nb_divide*/
  806.     (binaryfunc)int_mod, /*nb_remainder*/
  807.     (binaryfunc)int_divmod, /*nb_divmod*/
  808.     (ternaryfunc)int_pow, /*nb_power*/
  809.     (unaryfunc)int_neg, /*nb_negative*/
  810.     (unaryfunc)int_pos, /*nb_positive*/
  811.     (unaryfunc)int_abs, /*nb_absolute*/
  812.     (inquiry)int_nonzero, /*nb_nonzero*/
  813.     (unaryfunc)int_invert, /*nb_invert*/
  814.     (binaryfunc)int_lshift, /*nb_lshift*/
  815.     (binaryfunc)int_rshift, /*nb_rshift*/
  816.     (binaryfunc)int_and, /*nb_and*/
  817.     (binaryfunc)int_xor, /*nb_xor*/
  818.     (binaryfunc)int_or, /*nb_or*/
  819.     0,        /*nb_coerce*/
  820.     (unaryfunc)int_int, /*nb_int*/
  821.     (unaryfunc)int_long, /*nb_long*/
  822.     (unaryfunc)int_float, /*nb_float*/
  823.     (unaryfunc)int_oct, /*nb_oct*/
  824.     (unaryfunc)int_hex, /*nb_hex*/
  825. };
  826.  
  827. PyTypeObject PyInt_Type = {
  828.     PyObject_HEAD_INIT(&PyType_Type)
  829.     0,
  830.     "int",
  831.     sizeof(PyIntObject),
  832.     0,
  833.     (destructor)int_dealloc, /*tp_dealloc*/
  834.     (printfunc)int_print, /*tp_print*/
  835.     0,        /*tp_getattr*/
  836.     0,        /*tp_setattr*/
  837.     (cmpfunc)int_compare, /*tp_compare*/
  838.     (reprfunc)int_repr, /*tp_repr*/
  839.     &int_as_number,    /*tp_as_number*/
  840.     0,        /*tp_as_sequence*/
  841.     0,        /*tp_as_mapping*/
  842.     (hashfunc)int_hash, /*tp_hash*/
  843. };
  844.  
  845. void
  846. PyInt_Fini()
  847. {
  848.     PyIntObject *p;
  849.     PyIntBlock *list, *next;
  850.     int i;
  851.     int bc, bf;    /* block count, number of freed blocks */
  852.     int irem, isum;    /* remaining unfreed ints per block, total */
  853.  
  854. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  855.         PyIntObject **q;
  856.  
  857.         i = NSMALLNEGINTS + NSMALLPOSINTS;
  858.         q = small_ints;
  859.         while (--i >= 0) {
  860.                 Py_XDECREF(*q);
  861.                 *q++ = NULL;
  862.         }
  863. #endif
  864.     bc = 0;
  865.     bf = 0;
  866.     isum = 0;
  867.     list = block_list;
  868.     block_list = NULL;
  869.     free_list = NULL;
  870.     while (list != NULL) {
  871.         bc++;
  872.         irem = 0;
  873.         for (i = 0, p = &list->objects[0];
  874.              i < N_INTOBJECTS;
  875.              i++, p++) {
  876.             if (PyInt_Check(p) && p->ob_refcnt != 0)
  877.                 irem++;
  878.         }
  879.         next = list->next;
  880.         if (irem) {
  881.             list->next = block_list;
  882.             block_list = list;
  883.             for (i = 0, p = &list->objects[0];
  884.                  i < N_INTOBJECTS;
  885.                  i++, p++) {
  886.                 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
  887.                     p->ob_type = (struct _typeobject *)
  888.                         free_list;
  889.                     free_list = p;
  890.                 }
  891. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  892.                 else if (-NSMALLNEGINTS <= p->ob_ival &&
  893.                      p->ob_ival < NSMALLPOSINTS &&
  894.                      small_ints[p->ob_ival +
  895.                             NSMALLNEGINTS] == NULL) {
  896.                     Py_INCREF(p);
  897.                     small_ints[p->ob_ival +
  898.                            NSMALLNEGINTS] = p;
  899.                 }
  900. #endif
  901.             }
  902.         }
  903.         else {
  904.             PyMem_FREE(list); /* XXX PyObject_FREE ??? */
  905.             bf++;
  906.         }
  907.         isum += irem;
  908.         list = next;
  909.     }
  910.     if (!Py_VerboseFlag)
  911.         return;
  912.     fprintf(stderr, "# cleanup ints");
  913.     if (!isum) {
  914.         fprintf(stderr, "\n");
  915.     }
  916.     else {
  917.         fprintf(stderr,
  918.             ": %d unfreed int%s in %d out of %d block%s\n",
  919.             isum, isum == 1 ? "" : "s",
  920.             bc - bf, bc, bc == 1 ? "" : "s");
  921.     }
  922.     if (Py_VerboseFlag > 1) {
  923.         list = block_list;
  924.         while (list != NULL) {
  925.             for (i = 0, p = &list->objects[0];
  926.                  i < N_INTOBJECTS;
  927.                  i++, p++) {
  928.                 if (PyInt_Check(p) && p->ob_refcnt != 0)
  929.                     fprintf(stderr,
  930.                 "#   <int at %lx, refcnt=%d, val=%ld>\n",
  931.                       (long)p, p->ob_refcnt, p->ob_ival);
  932.             }
  933.             list = list->next;
  934.         }
  935.     }
  936. }
  937.